package cn.theten52.demo.patterns.design.creation.factory.asyn;

import cn.theten52.demo.patterns.design.creation.factory.abstract1.BigCarBody;
import cn.theten52.demo.patterns.design.creation.factory.abstract1.CarBody;
import cn.theten52.demo.patterns.design.creation.factory.abstract1.CarColor;
import cn.theten52.demo.patterns.design.creation.factory.abstract1.RedCarColor;
import cn.theten52.demo.patterns.design.creation.factory.abstract1.update1.CarLights;
import cn.theten52.demo.patterns.design.creation.factory.abstract1.update1.SquareCarLights;

import java.util.concurrent.*;

/**
 * 包含了一个异步生成车车身的工厂方法
 *
 * @author wangjin
 * @date 2020/12/27
 */
public class AsyncFactoryImpl implements AsyncFactory {
    private static final int POOL_SIZE = 3;

    private final ExecutorService executorService = new ThreadPoolExecutor(
        2,
        3,
        1,
        TimeUnit.MINUTES,
        new ArrayBlockingQueue<>(POOL_SIZE),
        new ThreadPoolExecutor.AbortPolicy()
    );

    /**
     * 异步生成车身
     *
     * @return 异步生成车身
     */
    @Override
    public Future<CarBody> asynchronousNewCarBody() {
        return executorService.submit(LongTimeCarBody::new);
    }

    /**
     * 生成车灯
     *
     * @return 车灯
     */
    @Override
    public CarLights newCarLights() {
        return new SquareCarLights();
    }

    /**
     * 生成车身
     *
     * @return 车身
     */
    @Override
    public CarBody newCarBody() {
        return new BigCarBody();
    }

    /**
     * 生成车辆颜色
     *
     * @return 车辆颜色
     */
    @Override
    public CarColor newCarColor() {
        return new RedCarColor();
    }
}
